package club.banyuan.hrms.servlet;

import club.banyuan.hrms.dao.AdminDao;
import club.banyuan.hrms.domain.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AdminServlet extends HttpServlet {
    private AdminDao adminDao;

    @Autowired
    public AdminServlet(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    public AdminDao getAdminDao() {
        return adminDao;
    }

    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        if (id != null) {
            Admin admin = adminDao.getAdminById(Integer.parseInt(id));
            if (admin != null) {
                // 将model 传入jsp
                // 方案1
                request.setAttribute("admin", admin);
                // 方案2
                request.setAttribute("username", admin.getUsername());
                request.setAttribute("rolename", admin.getRolename());
                request.setAttribute("password", admin.getPassword());
            }
        }

        // 跳转到页面
        request.getRequestDispatcher("admin.jsp?id=" + id).forward(request, response);
    }
}